package com.ajk.server.service.impl;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ajk.server.dao.PartMapper;
import com.ajk.server.exception.AjkException;
import com.ajk.server.exception.ErrorConstant;
import com.ajk.server.model.ReportPart;
import com.ajk.server.model.ReportPartAdvice;
import com.ajk.server.model.ReportPartHealthy;
import com.ajk.server.service.PartService;
import com.ajk.server.utils.FileStoreUtil;

@Service
public class PartServiceImpl implements PartService{

	@Autowired
	private PartMapper partMapper;
	
	@Override
	@Transactional
	public Integer addPart(ReportPart reportPart) {
		if(reportPart.getName() == null) {
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(),"名称不能为空");
		}
		if(StringUtils.isEmpty(reportPart.getFilename())) {
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(),"文件名称不能为空");
		}
		if(reportPart.getPartNo() == null) {
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(),"部位编号不能为空");
		}
	    partMapper.insertPart(reportPart);
	    Integer partId = reportPart.getId();
		if(CollectionUtils.isNotEmpty(reportPart.getAdviceList()) ) {
			for(ReportPartAdvice advice : reportPart.getAdviceList()) {
				advice.setPartId(partId);
				partMapper.insertPartAdvice(advice);
			}
		}
		if(reportPart.getReportPartHealthy() != null && StringUtils.isNotEmpty(reportPart.getReportPartHealthy().getContent()) ) {
			ReportPartHealthy reportPartHealthy =reportPart.getReportPartHealthy();
			reportPartHealthy.setPartId(partId);
			partMapper.insertPartHealthy(reportPartHealthy);
		}
		return partId;
	}

	@Override
	public Integer updatePart(ReportPart reportPart) {
		if(reportPart.getName() == null) {
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(),"名称不能为空");
		}
		if(StringUtils.isEmpty(reportPart.getFilename())) {
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(),"文件名称不能为空");
		}
		if(reportPart.getId() == null) {
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(),"ID不能为空");
		}
		partMapper.updatePart(reportPart);
		if(CollectionUtils.isNotEmpty(reportPart.getAdviceList()) ) {
			for(ReportPartAdvice advice : reportPart.getAdviceList()) {
				advice.setPartId(reportPart.getId());
				if(advice.getLevel() == null) {
					throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(),"参数 level 不能为空");
				}
				ReportPartAdvice peportPartAdvice = partMapper.getPartAdviceByLevelAndPartId(reportPart.getId(), advice.getLevel());
				if(null == peportPartAdvice) {
					partMapper.insertPartAdvice(advice);
				}else {
					partMapper.updatePartAdvice(advice);
				}
			}
		}
		if(reportPart.getReportPartHealthy() != null && StringUtils.isNotEmpty(reportPart.getReportPartHealthy().getContent()) ) {
			ReportPartHealthy reportPartHealthy =reportPart.getReportPartHealthy();
			reportPartHealthy.setPartId(reportPart.getId());
			ReportPartHealthy rph = partMapper.getReportPartHealthy(reportPart.getId());
			if(null == rph) {
				partMapper.insertPartHealthy(reportPartHealthy);
			}else {
				partMapper.updatePartHealthy(reportPartHealthy);
			}
		}
		return 1;
	}
	
	@Override
	public Integer deletePart(ReportPart reportPart) {
		if(reportPart.getId() == null) {
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(),"ID不能为空");
		}
		reportPart.setStatus(0);
		partMapper.updatePart(reportPart);
		return 1;
	}

	@Override
	public List<ReportPart> getPartList(ReportPart reportPart) {
		reportPart.setStatus(1);
		reportPart.setStart(null);
		List<ReportPart> partList = partMapper.getPartList(reportPart);
		for(ReportPart part : partList){
			if(null != part.getPartImage()) {
				part.setPartImage(FileStoreUtil.getBaseUrl().concat(part.getPartImage()));
			}
		}
		return partMapper.getPartList(reportPart);
	}

	@Override
	public ReportPart getPartById(Integer id) {
		ReportPart reportPart = partMapper.getPartById(id);
		if(null != reportPart) {
			List<ReportPartAdvice> adviceList = partMapper.getPartAdviceList(reportPart.getId());
			reportPart.setAdviceList(adviceList);
			
			ReportPartHealthy reportPartHealthy  = partMapper.getReportPartHealthy(reportPart.getId());
			reportPart.setReportPartHealthy(reportPartHealthy);
		}
		return reportPart;
	}
	
	

	@Override
	public ReportPart getReportBpartById(Integer id) {
		ReportPart reportPart = partMapper.getPartById(id);
		return reportPart;
	}

	@Override
	public ReportPartHealthy getReportPartHealthy(Integer partId) {
		
		return partMapper.getReportPartHealthy(partId);
	}

	@Override
	public ReportPartAdvice getPartAdviceByLevelAndPartId(Integer partId, Integer level) {
		ReportPartAdvice peportPartAdvice = partMapper.getPartAdviceByLevelAndPartId(partId, level);
		return peportPartAdvice;
	}
	
	
	
	
}